package nl.twingate.processrevivor.logic;

import java.util.List;


import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;

/**
     * This task will revive processes.
     * 
     * Please provide Resources > Arrays > Defaults with *packagename*;*packagename.app_main_activity* pairs
     * 
     * @author Marijn Oosterveld
     *
     */
    public class ReviveProcessesByReceiverTask extends AsyncTask<String, String, Void>{
    	
    	Context source;
    	String packageName;
    	String mainActivity;
    	int timeoutBeforeRevive = 2000;
    	String tag = "reviveProcessesTask";
    	
    	/*
    	 * Creates a new reviveProcesstask usign a context as a parent
    	 * From this parent, resources will be loaded to determine which processes will be revived.
    	 */
    	public ReviveProcessesByReceiverTask (Context source, String packageName, String mainActiviy, int timoutBeforeRevive){
    		this.source = source;
    		this.packageName = packageName;
    		this.mainActivity = mainActiviy;
    		this.timeoutBeforeRevive = timoutBeforeRevive;
    	}

    	/*
    	 * Revive our processes in the background
    	 */
		@Override
		protected Void doInBackground(String... paramsa) {
			
			//Sleep the timeout
			try {
				Thread.sleep(timeoutBeforeRevive);
			} catch (InterruptedException e1) {
				//Do nothing
			}
			
			
			//Check which processes are currently running:
			ActivityManager man = (ActivityManager) source.getSystemService(source.ACTIVITY_SERVICE);
			List<RunningAppProcessInfo> currentlyrunningapps = man.getRunningAppProcesses();
			
			//Look into the activitymanager if this process is already running
			// If the process is running, then ok!
			boolean running = false;
			for(RunningAppProcessInfo info : currentlyrunningapps){
				if(info.processName.equals(packageName)){
					running = true;
					Log.i(tag, packageName + " IS RUNNING");
				}
				
			}
			
			//If the process was not running, revive it.
			if(!running){
				Log.w(tag, packageName + " IS  NOT RUNNING, reviving..");
				
				try{
					Intent intent = new Intent();
					intent.setComponent(new ComponentName(packageName, mainActivity));
					intent.setFlags(intent.FLAG_ACTIVITY_CLEAR_TOP);
					intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK);
					source.startActivity(intent);
				}catch(Exception e){
					Log.w(tag, "Could not start a process" + e.getMessage());
				}
			}

			
			return null;
		}
		
		/*
		 * (non-Javadoc)
		 * @see android.os.AsyncTask#onProgressUpdate(Progress[])
		 */
		@Override
		protected void onProgressUpdate(String... values) {
			super.onProgressUpdate(values);
		}
		
		/*
		 * (non-Javadoc)
		 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
		 */
		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			
			try {
				this.finalize();
			} catch (Throwable e) {
				e.printStackTrace();
			}
		}
    	
    }